package demo5;

import java.util.*;

public class Student  {
    private String name;
    private int age;
    private float grade;

    public Student() {
    }

    public Student(String name, int age, float grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                '}';
    }
    private static List<Student>students=new ArrayList<>();
    static {
        students.add(new Student("张三",20,90.0F));
        students.add(new Student("李四",22,90.0F));
        students.add(new Student("王五",20,95.0F));
        students.add(new Student("赵六",22,100.0F));
        students.add(new Student("田七",26,87.0F));
    }

    public static void main(String[] args) {
        Iterator it=students.iterator();
        while (it.hasNext()){
            Student student=(Student) it.next();
            System.out.println(student);
        }
        System.out.println("********************************");
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result=(int)(o2.getGrade()-o1.getGrade());
                if (result==0){
                    result=o2.getAge()-o1.getAge();
                }
                return result;
            }
        });
        for (Student s:students) {
            System.out.println(s);
        }
    }
}
